package com.lzx.iteam.net;

import android.os.Message;
import com.lzx.iteam.AttendanceCountActivity;
import com.lzx.iteam.bean.AttendanceCountData;
import com.lzx.iteam.util.Constants;
import com.lzx.iteam.util.StringUtil;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by Megan on 16/4/18.
 */
public class GetAttendanceCountMsg extends CcMsgStructure {

    private Message mCallback;
    private ArrayList<AttendanceCountData> attendanceCountDatas = new ArrayList<AttendanceCountData>();

    public GetAttendanceCountMsg(Message callback) {
        mCallback = callback;
    }

    @Override
    public void onReceive(JSONObject data) {
        try {
            JSONObject jsonObject = data.getJSONObject(AsynHttpClient.KEY_CC_DATA);
            JSONArray members = jsonObject.getJSONArray("members");
            for (int i = 0; i < members.length(); i++) {
                AttendanceCountData attendanceCountData = new AttendanceCountData();
                JSONObject item = members.getJSONObject(i);
                attendanceCountData.setUserId(item.getString("user_id"));
                attendanceCountData.setUserName(item.getString("user_name"));
                if (item.has("user_image")) {
                    attendanceCountData.setUserImage(item.getString("user_image"));
                }
                attendanceCountData.setLeaveType0(item.getString("approval_leave_type_0"));
                attendanceCountData.setLeaveType1(item.getString("approval_leave_type_1"));
                attendanceCountData.setLeaveType2(item.getString("approval_leave_type_2"));
                attendanceCountData.setLeaveType3(item.getString("approval_leave_type_3"));
                attendanceCountData.setLeaveType4(item.getString("approval_leave_type_4"));
                attendanceCountData.setWorkBeLateDay(item.getString("work_be_late"));
                if (item.has("work_be_late_count")) {
                    attendanceCountData.setWorkBeLateCount(item.getString("work_be_late_count"));
                }
                attendanceCountData.setWorkLeaveEarlyDay(item.getString("work_leave_early"));
                if (item.has("work_leave_early_count")) {
                    attendanceCountData.setWorkLeaveEarlyCount(item.getString("work_leave_early_count"));
                }
                if (item.has("legwork_count")) {
                    attendanceCountData.setLegworkCount(item.getString("legwork_count"));
                }
                attendanceCountData.setOutDay(item.getString("approval_out"));
                attendanceCountData.setBusinessDay(item.getString("approval_business"));
                attendanceCountData.setWorkDay(item.getString("work_day"));
                attendanceCountData.setLeaveDay(item.getString("approval_leave"));
                if (item.has("un_work")){
                    attendanceCountData.setUnWorkDay(item.getString("un_work"));
                }
                attendanceCountData.setTotal(item.getString("total"));
                attendanceCountDatas.add(attendanceCountData);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        if (data != null) {
            if (mCallback != null) {
                mCallback.arg1 = 0;
                mCallback.obj = attendanceCountDatas;
                mCallback.sendToTarget();
            }
        } else {
            if (mCallback != null) {
                mCallback.arg1 = 2;
                mCallback.sendToTarget();
            }
        }
    }

    @Override
    public void onError(int error, Object ret) {
        String errorMsg = null;
        if (ret != null) {
            JSONObject jData = null;
            try {
                jData = new JSONObject(ret.toString());
                errorMsg = jData.getString("msg");
            } catch (JSONException e) {
                errorMsg = "服务端数据解析失败!";
                e.printStackTrace();
            }

        }
        if (mCallback != null) {
            mCallback.arg1 = error;
            if (!StringUtil.isEmpty(errorMsg)) {
                mCallback.obj = errorMsg;
            } else {
                mCallback.obj = Constants.NETWORK_WRROR;
            }
            mCallback.sendToTarget();
        }
    }
}